package com.atguigu.testdemo.customer.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.testdemo.customer.bean.Customer;
import com.atguigu.testdemo.customer.service.CustomerService;
import com.atguigu.testdemo.customer.service.impl.CustomerServiceImpl2;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;


@RestController
@DS("test")
public class CustomerController {

    //ustomerServiceImpl2 customerServiceImpl2 = new CustomerServiceImpl2();

    @Autowired
    @Qualifier("csi")
    CustomerService customerService;

    @PostMapping("/InsertCustomerTest/{name}/{age}")
    public String insertCustomerTest(@PathVariable("name") String name , @PathVariable("age") Integer age){
        customerService.insertCustomerTest(name , age);
        return "success";
    }

    @GetMapping("/getCustomerByName/{name}/{age}")
    public Customer getCustomerByName(@PathVariable("name") String name,@PathVariable("age") Integer age){
        Customer customer = customerService.getCustomer(name,age);
        return customer;
    }

    @PostMapping("/deletecustomer/{id}")
    public String deleteCustomer(@PathVariable Long id){
        customerService.deleteCustomer(id);
        return "success";
    }

    @PostMapping("updatecustomer")
    public String updatecustomer(@RequestBody Customer customer){
        customerService.updateCustomer(customer);
        return "success";
    }

    @PostMapping(value = "/savecustomer")
    public String saveCustomer(@RequestBody Customer customer) {
        customerService.saveCustomer(customer);
        return "success";
    }

    @GetMapping("/getcustomer/{id}")
    public Customer getCustomer(@PathVariable ("id") Long id) {
       Customer customer = customerService.getCustomer(id);

        return customer;
    }


    @GetMapping("/code")
    public String code(@RequestParam(value = "username",required = false,defaultValue = "wuming") String username,
                       @RequestParam(value = "age",required = false,defaultValue = "18") Integer age){
        System.out.println("username:" + username + ",age:" + age);
        if(1 == 1){
            throw new RuntimeException("抛异常了.....");
        }
        return "success";
    }

    @GetMapping("/result")
    public Customer result(){
        return new Customer(1001L,"zhangsan",20);
        //return JSON.toJSONString(customer);
    }


    @RequestMapping(value = "/requestmethod",method =  RequestMethod.GET)
    //@GetMapping(value = "/requestmethod",)
    public String requestmethod(){
        return "success";
    }

    //http://localhost:8080/param3  {"id":1001, "name":"zhaoliu","age":26}
    @RequestMapping("/param3")
    public String param3(@RequestBody Customer customer) {
        System.out.println("customer " + customer);
        return "success";
    }

    //http://localhost:8080/param2/wnagwu/30
    @RequestMapping("/param2/{name}/{age}")
    public String param2(@PathVariable("name") String userName ,@PathVariable("age") Integer age){
        System.out.println("姓名：" + userName + "，年龄：" + age);
        return "success";
    }

    //http://localhost:8080/param1?username=zhangsan&age=20
    @RequestMapping(value = "/param1")
    public String param1(@RequestParam(value = "username") String userName,@RequestParam(value = "age") Integer age){
        System.out.println("userName:" + userName + ",age: " + age);
        return "success";
    }

    //http://localhost:8080/hello
    @RequestMapping(value = "/hello")
    public String hello(){
        System.out.println("客户端有请求发送过来了");
        return "hello";
    }
}
